{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href='http://www.holoviews.org'><img src=\"../../assets/hv+bk.png\" alt=\"HV+BK logos\" width=\"40%;\" align=\"left\"/></a>\n",
    "<div style=\"float:right;\"><h2>Exercise 1: Making Data Visualizable</h2></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import holoviews as hv\n",
    "\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "macro = pd.read_csv('../../data/macro.csv')\n",
    "us_macro = macro[macro.country=='United States']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Have a look at the ``us_macro`` dataframe, using ``us_macro.head()``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now plot the 'unem' column against the 'year' column using a ``Curve`` element:\n",
    "\n",
    "<b><a href=\"#hint1\" data-toggle=\"collapse\">Hint</a></b>\n",
    "\n",
    "<div id=\"hint1\" class=\"collapse\">\n",
    "When declaring an element list the independent dimensions (or ``kdims``) first and the dependent dimensions (or ``vdims``) second.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b><a href=\"#solution1\" data-toggle=\"collapse\">Solution</a></b>\n",
    "\n",
    "<div id=\"solution1\" class=\"collapse\">\n",
    "    <br>\n",
    "    <code>hv.Curve(us_macro, 'year', 'unem')</code>\n",
    "</div>\n",
    "\n",
    "Finally overlay the ``Curve`` with a ``Scatter`` element plotting the same data. Then adjust the ``width`` of the plot and increase the ``size`` of the ``Scatter``.\n",
    "\n",
    "<b><a href=\"#hint2\" data-toggle=\"collapse\">Hint</a></b>\n",
    "\n",
    "<div id=\"hint2\" class=\"collapse\">\n",
    "Plot options like ``width`` are specified using square brackets (``[]``), while style options like ``size`` are specified using parentheses (``()``)\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "                                                                                                                                   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b><a href=\"#solution2\" data-toggle=\"collapse\">Solution</a></b>\n",
    "\n",
    "<div id=\"solution2\" class=\"collapse\">\n",
    "<br>\n",
    "<code>%%opts Scatter [width=600] (size=6)\n",
    "(hv.Curve(us_macro, 'year', 'unem') \\* hv.Scatter(us_macro, 'year', 'unem'))\n",
    "</code>\n",
    "<br>\n",
    "(where the width can be set on any out of ``Curve``, ``Scatter``, or ``Overlay`` with the same effect, but the size needs to be set on the Scatter for it to work.  You can also use ``.options()``:\n",
    "<br>\n",
    "<code>\n",
    "hv.Curve(us_macro,'year','unem') \\* hv.Scatter(us_macro,'year','unem').options(width=600, size=10)</code>\n",
    "<br>\n",
    "<br>\n",
    "And you can make the ranges a bit nicer if you want:\n",
    "<br>\n",
    "<br>\n",
    "<code>%%opts Scatter [width=600] (size=6)\n",
    "(hv.Curve(us_macro, 'year', 'unem') \\* hv.Scatter(us_macro, 'year', 'unem')).redim.range(unem=(2, 10))\n",
    "</code>\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dates = pd.date_range('2017-01-01', '2017-11-14')\n",
    "values = np.cumsum(np.random.randn(len(dates)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Display the ``values`` for the given ``dates`` using a ``Curve`` element, and name the x-axis and y-axis.\n",
    "\n",
    "<b><a href=\"#hint3\" data-toggle=\"collapse\">Hint</a></b>\n",
    "\n",
    "<div id=\"hint3\" class=\"collapse\">\n",
    "When declaring an Element with individual columns as arrays, lists or Series wrap them in a tuple, e.g. (column1, column2). Or you can make a dataframe out of it first and pass that.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b><a href=\"#solution3\" data-toggle=\"collapse\">Solution</a></b>\n",
    "\n",
    "<div id=\"solution3\" class=\"collapse\">\n",
    "<br>\n",
    "<code>hv.Curve((dates, values), 'Date', 'Value').relabel('Timeseries')</code>\n",
    "<br><br>\n",
    "or\n",
    "<br><br>\n",
    "<code>hv.Curve(pd.DataFrame(dict(Date=dates,Value=values)))</code>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now display a ``Curve`` of the ``'trade'`` column by ``'year'`` next to a ``Histogram`` of the 'trade'. Then change the ``color`` and ``width`` of the ``Curve`` and the ``fill_color`` of the Histogram.\n",
    "\n",
    "<b><a href=\"#hint4\" data-toggle=\"collapse\">Hint</a></b>\n",
    "\n",
    "<div id=\"hint4\" class=\"collapse\">\n",
    "To declare the Histogram use ``np.histogram`` to compute the histogram values and declare the ``kdims``. \n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b><a href=\"#solution4\" data-toggle=\"collapse\">Solution</a></b>\n",
    "\n",
    "<div id=\"solution4\" class=\"collapse\">\n",
    "<br>\n",
    "<code>%%opts Curve [width=600] (color='indianred') Histogram (fill_color='darkgreen')\n",
    "hv.Curve(us_macro, 'year', 'trade') + hv.Histogram(np.histogram(us_macro.trade), kdims='Trade')</code>\n",
    "<br><br>\n",
    "or, for an adjoint histogram:\n",
    "<br><br>\n",
    "<code>hv.Curve(us_macro, 'year', 'trade').hist()</code>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&nbsp;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
